//
//  BookViewController.m
//  Qyer
//
//  Created by 赵宇 on 15/9/24.
//  Copyright (c) 2015年 赵宇. All rights reserved.
//

#import "BookViewController.h"
#import "BaseHeader.h"
@interface BookViewController ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>

@end

@implementation BookViewController
-(void)dealloc
{
    [_searArr release];
    [_search release];
    [_table release];
    [super dealloc];
}
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    _searArr = [[NSMutableArray alloc]init];
    [self creatNavigation];
    
}
-(void)creatNavigation
{
    UIView *nav = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    nav.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:nav];
    
    self.search = [[UISearchBar alloc]initWithFrame:CGRectMake(30, 25, SCREEN_WIDTH - 110, 30)];
    self.search.tintColor = [UIColor grayColor];
    self.search.barTintColor = [UIColor whiteColor];
    self.search.layer.borderColor = [[UIColor grayColor]CGColor];
    self.search.layer.borderWidth = .7;
    self.search.layer.masksToBounds = YES;
    self.search.layer.cornerRadius = 13.;
    self.search.placeholder = @"请输入想要搜索的帖子";
    [self.search setDelegate:self];
    [nav addSubview:self.search];
    
    
    UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancel setTitle:@"取消" forState:UIControlStateNormal];
    [cancel setTitleColor:[UIColor colorWithRed:34./255 green:169./ 255 blue:118./255 alpha:1] forState:UIControlStateNormal];
    cancel.frame = CGRectMake(SCREEN_WIDTH-70, 25,40 , 30);
    [cancel addTarget:self action:@selector(returnBefore) forControlEvents:UIControlEventTouchUpInside];
    cancel.titleLabel.font = [UIFont systemFontOfSize:14.];
    [nav addSubview:cancel];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 63, SCREEN_WIDTH, .7)];
    line.backgroundColor = [UIColor blackColor];
    [nav addSubview:line];
    
    [self.view addSubview:nav];
    [nav release];
    [line release];
}
-(void)returnBefore
{
    [_search resignFirstResponder];
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)getData
{
    _searArr = [[NSMutableArray alloc]init];
    
    NSString *searchStr = _search.text;
    if (![searchStr isEqualToString:@""]) {
        
        
        NSString *string = [NSString stringWithFormat:@"http://open.qyer.com/qyer/search/index?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&keyword=%@&oauth_token=d5ecb9febb61510d54ba2d47d986cf65&page=1&place_field=country|city|poi&track_app_version=6.6.6&track_deviceid=301FBCE0-A789-4EAC-ADF1-95E1AACF2A70&track_user_id=6695170&type=thread&v=1",searchStr];
        
        string = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [AFNblock getdataWithString:string block:^(id block) {
            NSDictionary *bigDic = block;
            NSDictionary *dataDic = [bigDic objectForKey:@"data"];
            NSArray *entryArr =[dataDic objectForKey:@"entry"];
            for (NSDictionary *dic in entryArr) {
                
                LookHotTravelModel *model = [[LookHotTravelModel alloc]initWithDictionary:dic];
                
                [_searArr addObject:model];
                
            }
            
            if (_searArr.count != 0) {
                _table.hidden = NO;
                if (_table == nil) {
                    
                    [self creatTableView];
                }
                [_table reloadData];
            }else{
                _table.hidden = YES;
                [self nonInfor];
            }
        }];
        
    } [_table reloadData];
}
-(void)nonInfor
{
    self.lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 104, SCREEN_WIDTH, 40)];
    self.lab.text = @"对不起没有相关内容";
    self.lab.textAlignment = YES;
    [self.view addSubview:self.lab];
}

#pragma mark - UISearchBarDelegate
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    // 当textView的文字发生改变时调用此方法,搜索栏目前的状态是正在编辑,在搜索文本字段中的当前文本
    if (![searchText isEqualToString:@""]) {
        self.lab.hidden = YES;
    }
    
    [self getData];
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.search resignFirstResponder];
}

#pragma mark - UITableViewDelegate
-(void)creatTableView
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT- 64) style:UITableViewStylePlain];
    [self.table setDelegate:self];
    [self.table setDataSource:self];
    [self.view addSubview:self.table];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _searArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_HEIGHT*0.157;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *bookCellid = @"bookCellID";
    BookTableViewCell *bookCell = [tableView dequeueReusableCellWithIdentifier:bookCellid];
    if (!bookCell) {
        bookCell = [[BookTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:bookCellid];
    }
    bookCell.selectionStyle = UITableViewCellSelectionStyleNone;
   LookHotTravelModel *model = [_searArr objectAtIndex:indexPath.row];
    [bookCell setValueWithLookHotModel:model];
    if (_searArr.count > 0) {
        
        // 搜索关键字高亮
        NSMutableAttributedString *arrtriSrt = [[NSMutableAttributedString alloc]initWithString:model.title];
        NSRange range = [model.title rangeOfString:_search.text];
        [arrtriSrt addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
        bookCell.titleLab.attributedText = arrtriSrt;

    }
    
    return bookCell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    LookHotTravelModel *Lmod = [_searArr objectAtIndex:indexPath.row];
    SlideViewController *web = [[SlideViewController alloc]init];
    web.url = Lmod.view_url;
    [self.navigationController pushViewController:web animated:YES];
    
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [_search resignFirstResponder];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
